<?php
namespace app\shell;

use app\model\UrlDomainSelectorModel;
use share\console\CLIRunner;
use app\service\UrlConfigService;
use app\service\UrlDomainRelationService;
use app\service\UrlDomainSelectorService;
use app\service\UrlDomainService;
use app\service\UrlSelectorService;
use app\service\UrlService;
use share\console\CConsole;
use share\utils\Helper;

class CUrl extends CLIRunner{

    public function test_action(){ 
        $data = UrlConfigService::getAllConfig();
        return $data;
    }

    public function rebuild_domain_priority_action(){
        // $data = UrlDomainSelectorService::getAllByFilter([]);
        // $updateInfo = [];
        // foreach($data as $item){
        //     $score = UrlDomainSelectorService::getScoreByType($item['type']);
        //     $id = $item['url_domain_id'];
        //     if(!isset($updateInfo[$id])){
        //         $updateInfo[$id] = 0;
        //     }
        //     $updateInfo[$id] += $score;
        // }
        // if(count($updateInfo) > 0){
        //     foreach($updateInfo as $id => $score){
        //         UrlDomainService::update($id,[
        //            'priority' => $score, 
        //         ]);
        //     }            
        // }
        // UrlConfigService::deleteCache();
    }

    public function test_mapping_action(){
        $filter = [
            'ip' => '161.117.140.196',
            'vendor' => 'enz',
            'package' => 'com.antsvision.seeeasy',
            'version' => '112212',
            'clientType' => '',
            'client' => '',
            'did' => 'a6e83037-9257-487b-9099-03c745892c22',
        ];
        $data = UrlService::getUrlInfoByFilter(
            $filter['ip'],
            $filter['vendor'],
            $filter['package'],
            $filter['version'],
            $filter['clientType'],
            $filter['client'],
            $filter['did']
        );
        return $data;
    }


    public function selector_data_repair_action(){
        $datas = UrlDomainSelectorModel::all([
            'type' => 'package',
        ]);
        $domains = UrlDomainService::getAllByFilter([]);
        $selectors = [];
        foreach($datas as $data){
            $packages = json_decode($data['includes'],true);
            $domainId = $data['url_domain_id'];
            $domain = Helper::filterOne($domains,['id' => $domainId]);
            if(empty($domain)){
                CConsole::print_line("domain {domainId} is not exist. package:{package}" , [
                    '{domainId}' => $domainId,
                    '{package}' => $data['includes'],
                ]);
                continue;
            }
            $domainType = $domain['type'];
            foreach($packages as $package){
                if(isset($selectors[$package])){
                    $selectors[$package][$domainType] = $domainId;
                }
                else{
                    $selectors[$package] = [
                        'id' => 0,
                        'key' => 'package',
                        'value' => $package,
                        $domainType => $domainId,
                    ];
                }                
            }
        }
        foreach($selectors as $updateData){
            if(count($updateData) < 10) {
                CConsole::print_line("select not fill full . data: {{data}}" , [
                    '{data}' => json_encode($updateData,JSON_UNESCAPED_SLASHES),                   
                ]);
                continue;
            }
            $update = [
                'id' => 0,
                'account' => $updateData['account'],
                'appShare' => $updateData['appShare'],
                'burn' => $updateData['burn'],
                'h5' => $updateData['h5'],
                'oauth' => $updateData['oauth'],
                'sim' => $updateData['sim'],
                'oss' => $updateData['oss'],
                'selector' => [
                    [
                        'id' => 0,
                        'key' => $updateData['key'],
                        'value' => $updateData['value'],
                    ]
                ]
            ];
            UrlDomainRelationService::allocBySelector($update);
        }
        return $selectors;

    }
}